const fs = require('fs');
const router = require('koa-router')();
const fileUploader = require('../libs/FileUploadHelper');

router.prefix('/api/file');

router.post('/upload', async (ctx, next) => {
    let files = fileUploader.upload(ctx);

    await (ctx.body = JSON.stringify(files));
});

router.post('/upload/single', async (ctx, next) => {
    let file = fileUploader.upload(ctx)[0] || {};

    await (ctx.body = JSON.stringify({
        file,
        fields: ctx.request.body.fields
    }));
});


module.exports = (app) => {
    app.use(router.routes(), router.allowedMethods());

    return router;
};